//
//  CZStatusPictureView.swift
//  crkWeibo
//
//  Created by 陈荣科 on 15/12/28.
//  Copyright © 2015年 crk. All rights reserved.
//

import UIKit
import SDWebImage
class CZStatusPictureView: UICollectionView {

    
    // MARK: - 属性
    private let ReuseIdentifier = "ReuseIdentifier"
    
    ///微博模型，用于显示图片和计算配图视图的尺寸
    var status: CZStatus? {
    
        didSet{
        
            //防止cell重用
            self.reloadData()
            
        }
    
    }
    ///计算配图的尺寸，根据模型里面图片的数量来计算
    //计算好的宽高
    
    func calcViewSize() -> CGSize {
        
        //itemSize设置全局cell的尺寸
        let itemSize = CGSize(width: 90, height: 90)
        
        //间距
        let margin: CGFloat = 10
        
        layout.itemSize = itemSize
        
        //间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        //获得土坯那的数量，如果没有值,默认为0
        let count = status?.pictureURLs?.count ?? 0
        
        //判断图片的数量
        //没有图片
        if count == 0 {
            
            return CGSizeZero
        }
        //1张图片
        if count == 1{
            
            var size = CGSize(width: 150, height: 120)
            
            
            //到缓存中取的图片的原大小
            let url = status!.pictureURLs![0]
            
            //获取图片之前，使用SDWebImage缓存的图片，现在时用SDWebImage来加载图片
            if let image = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(url.absoluteString){
            
            size = image.size
            }
            //判断图片大小
            if size.width < 40 {
            
                size.width = 40
            }else if size.width > UIScreen.mainScreen().bounds.width * 0.5{
            
                size.width = UIScreen.mainScreen().bounds.width * 0.5
            }
            
            layout.itemSize = size

            return size
        }
        
        //4张图片
        if count == 4{
            
            let value = 2 * itemSize.width + margin
            
            return CGSize(width: value, height: value)
        }
        // 其他情况
        // 列数,行数计算依赖列数,column必须为Int
        let column = 3
        
        // 宽度 = 列数 * cell的宽度 + (列数 - 1) * 间距
        let width = CGFloat(column) * itemSize.width + (CGFloat(column) - 1) * margin
        
        // 行数 = (图片的数量(Int) + 列数(Int) - 1) / 列数(Int)
        /*
        1 + 3 - 1 / 3 = 1
        2 + 3 - 1 / 3 = 1
        3 + 3 - 1 / 3 = 1
        5 + 3 - 1 / 3 = 2
        6 + 3 - 1 / 3 = 2
        7 + 3 - 1 / 3 = 3
        8 + 3 - 1 / 3 = 3
        9 + 3 - 1 / 3 = 3
        */
        let row = (count + column - 1) / column
        
        // 高度 = 行数 * cell的高度 + (行数 - 1) * 间距
        let height = CGFloat(row) * itemSize.height + (CGFloat(row) - 1) * margin
        
        return CGSize(width: width, height: height)
    }
    
    
    ///设置流水布局
    private var layout = UICollectionViewFlowLayout()
    
    init() {
    
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        
        //注册cell
        
        self.registerClass(CZStatusPictureViewCell.self, forCellWithReuseIdentifier: ReuseIdentifier)
        //实现代理
        self.dataSource = self
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


///MARK: - 扩展CZStatusPictureView 实现UICollectionViewDataSource
extension CZStatusPictureView: UICollectionViewDataSource {
    
    //返回每组图片cell 的数量
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return status?.pictureURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! CZStatusPictureViewCell
        
//        //获取图片的url
//        let url = status?.pic_urls![indexPath.item]["thumbnail_pic"] as! String
//        cell.imageURL = NSURL(string: url)
        
        cell.imageURL = status?.pictureURLs![indexPath.item]

        return cell
    }

}

///自定义cell
class CZStatusPictureViewCell: UICollectionViewCell {

    ///MARK: - 属性
    
    ///要显示图片的url
    var imageURL: NSURL? {
    
        didSet {
        
            //显示图片
            imageView.sd_setImageWithURL(imageURL)
        }
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //准备UI
        prepareUI()
    }


    func prepareUI() {
    
        //添加子控件
       contentView.addSubview(imageView)
        
        //添加约束
        imageView.ff_Fill(contentView)
    
    }
    ///MARK: - 懒加载
    
    ///图片
    private lazy var imageView: UIImageView = UIImageView(image: nil)
}





